package org.onvivals.common.param;

import com.baomidou.mybatisplus.annotation.TableField;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.onvivals.common.annotation.QueryField;
import org.onvivals.common.annotation.QueryType;

import java.io.Serial;
import java.io.Serializable;
import java.util.List;

/**
 * 查询参数基本字段
 *
 * @author weirdor (179946422@qq.com)
 * @version v1.0.0
 */
@Data
public class BaseParam implements Serializable {

	@Serial
	private static final long serialVersionUID = 1L;

	@TableField(exist = false)
	@Schema(description = "分页查询页码")
	private Long page;

	@TableField(exist = false)
	@Schema(description = "分页查询每页数量")
	private Long limit;

	@TableField(exist = false)
	@Schema(description = "排序字段", example = "排序字段或sql, 如果是sql则order字段无用, 如: `id asc, name desc`")
	private String sort;

	@TableField(exist = false)
	@Schema(description = "排序方式", example = "sort是字段名称时对应的排序方式, asc升序, desc降序")
	private String order;

	@QueryField(value = "create_time", type = QueryType.GE)
	@TableField(exist = false)
	@Schema(description = "起始创建时间")
	private String startTime;

	/**
	 * 获取集合中的第一条数据
	 *
	 * @param records 集合
	 * @return 第一条数据
	 */
	public <T> T getOne(List<T> records) {
		return records == null || records.isEmpty() ? null : records.get(0);
	}
}
